/*
** File.cpp for 304Pacman in /home/mickael/Math
**
** Made by mickael gomes
** Login   <gomes.mickael@epitech.net>
**
** Started on  jeu. nov.  07 11:24:49 2013 mickael gomes
** Last update jeu. nov.  07 11:24:49 2013 mickael gomes
*/

#include					"File.hh"

File::File(char *fileName)
	: fileName_(fileName)
{
	height_ = 0;
	readFile();
	findGhost();
	convertMap();
}

File::~File()
{
	
}

int*						File::Remplissage(std::string line, int len, int *numLine)
{
	int						x = 0;

	while (x != len)
		{
			if (line[x] == '0')
				numLine[x++] = 0;
			else if (line[x] == '1')
				numLine[x++] = -1;
			else if (line[x] == 'D')
				numLine[x++] = -2;
			else
				numLine[x++] = -3;
		}
	return (numLine);
}

void						File::convertMap()
{
	std::vector<std::string>::iterator it = map_.begin();
	len_ 					= (*it).length();

	while (it != map_.end())
		numMap_.push_back(std::pair<int*, bool>(Remplissage(*(it++), len_, new int[len_]), false));
}


void						File::findGhost()
{
	std::vector<std::string>::iterator it = map_.begin();
	size_t					x = std::string::npos;
	int 					y = 0;

	while (x == std::string::npos && it != map_.end())
	{
		x = (*it).find("D");
		if (x == std::string::npos)
			y++;
		it++;
	}
	if (x == std::string::npos)
	{
		std::cout << "There's no ghost in this map" << std::endl;
		return ;
	}
	ghostCoord_[0] = x;
	ghostCoord_[1] = y;
}

void						File::readFile()
{
	std::ifstream 			file(fileName_, std::ios::in);
	std::string				line;

	while (getline(file, line))
		{
			map_.push_back(line);
			height_++;
		}
	file.close();
}

std::vector<std::string>	File::getMap()
{
	return (map_);
}

int*						File::getGhostCoord()
{
	return (ghostCoord_);
}

std::vector< std::pair<int*, bool> >		 &	File::getNumMap()
{
	return (numMap_);
}

int							File::getLen()
{
	return (len_);
}

int							File::getHeight()
{
	return (height_);
}